//
//  MenuViewController.m
//  南京大牌档
//
//  Created by student on 14-1-9.
//  Copyright (c) 2014年 无锡第三团队. All rights reserved.
//

#import "MenuViewController.h"
#import "AppDelegate.h"
#import "MenuCell.h"
#import "UIViewController+MJPopupViewController.h"
#import "DetialFoodViewController.h"

@interface MenuViewController ()
{
    DetialFoodViewController *detialFoodVC;
}

@end

@implementation MenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor=[UIColor whiteColor];
        
        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"<<菜类" style:UIBarButtonItemStylePlain target:self.viewDeckController action:@selector(toggleLeftView)];
        
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"清单>>" style:UIBarButtonItemStylePlain target:self.viewDeckController action:@selector(toggleRightView)];

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(goChange:) name:@"ChangeCenter" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(translateNum:) name:@"translateCellData" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNum:) name:@"translateRightCellData" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showDetialFoodVC:) name:@"showDetialFoodVC" object:nil];
        
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
    }
#endif
    
    foodMenuArray=[[NSMutableArray alloc]init];
    menuTV=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, WXHLApplicationBounds().size.height-64-49) style:UITableViewStylePlain];
    foodOrderArray=[[NSMutableArray alloc]init];
    foodNumArray=[[NSMutableArray alloc]init];
    [self initTableView];
     [self requestMenuData];
}
-(void)initTableView
{
    menuTV.delegate=self;
    menuTV.dataSource=self;
    menuTV.backgroundColor=[UIColor clearColor];
    menuTV.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:menuTV];
}
-(void)requestMenuData
{
    NSError *error;
    NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foodmenu" ofType:@"txt"]encoding:NSUTF16StringEncoding error: & error];
    NSArray *linesArray = [textFileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet ]];
    NSMutableArray *foodLinesArray=[[NSMutableArray alloc]init];
    for (int i=0; i<linesArray.count-linesArray.count/2; i++) {
        [foodLinesArray addObject:linesArray[i*2]];
    }
    elementArray=[[NSMutableArray alloc]initWithCapacity:linesArray.count];
    
    for (int i=0; i<foodLinesArray.count; i++) {
        [elementArray addObject:[foodLinesArray[i] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet ]]];
        [foodNumArray addObject:@0];
    }
    foodMenuArray=elementArray;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return foodMenuArray.count-foodMenuArray.count/2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer=@"Cell";
    MenuCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if(cell==nil)
    {
        cell=[[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (foodMenuArray.count/2!=0&&indexPath.row==foodMenuArray.count-foodMenuArray.count/2-1) {
        cell.titleLeftString=foodMenuArray[foodMenuArray.count-1][1];
        cell.priceLeftString=[NSString stringWithFormat:@"%@元/%@",foodMenuArray[indexPath.row*2][3],foodMenuArray[foodMenuArray.count-1][2]];
        cell.foodLeftString=foodMenuArray[foodMenuArray.count-1][5];
        cell.foodLeftArray=foodMenuArray[foodMenuArray.count-1];

        cell.titleRightString=@" ";
        cell.priceRightString=@" ";
        cell.foodRightString=@" ";
        cell.foodRightArray=[NSArray arrayWithObjects:@" ",@" ",@" ",@" ",@" ",@" ",nil];
        cell.orderRight.userInteractionEnabled=NO;
        cell.foodRightImage.userInteractionEnabled=NO;
    }
    else{
        
        cell.titleLeftString=foodMenuArray[indexPath.row*2][1];
        cell.titleRightString=foodMenuArray[indexPath.row*2+1][1];
        cell.priceLeftString=[NSString stringWithFormat:@"%@元/%@",foodMenuArray[indexPath.row*2][3],foodMenuArray[indexPath.row*2][2]];
        cell.priceRightString=[NSString stringWithFormat:@"%@元/%@",foodMenuArray[indexPath.row*2+1][3],foodMenuArray[indexPath.row*2+1][2]];
        cell.foodLeftString=foodMenuArray[indexPath.row*2][5];
        cell.foodRightString=foodMenuArray[indexPath.row*2+1][5];
        cell.foodLeftArray=foodMenuArray[indexPath.row*2];
        cell.foodRightArray=foodMenuArray[indexPath.row*2+1];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.deckController.leftSize=180;
    myDelegate.deckController.rightSize=30;
    [myDelegate.deckController closeLeftView];
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.deckController.leftSize=320;
    myDelegate.deckController.rightSize=320;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
}
-(void)translateNum:(NSNotification *)notification
{

    num+=1;
    self.navigationItem.title=[[NSString alloc]initWithFormat:@"共点%d道菜",num];

}

-(void)showDetialFoodVC:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];
    
    NSArray *array=[dic objectForKey:@"Array"];

    detialFoodVC = [[DetialFoodViewController alloc] init];

    detialFoodVC.detialFoodArray=array;
    int type=arc4random()%4;
    [self presentPopupViewController:detialFoodVC animationType:type];

   
   

    
}

-(void)changeNum:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];

    NSNumber *tag=[dic objectForKey:@"Tag"];
    
    if ([tag integerValue]==0) {
        num-=1;
        self.navigationItem.title=[[NSString alloc]initWithFormat:@"共点%d道菜",num];
    }
    else if([tag integerValue]==1){
        num+=1;
        self.navigationItem.title=[[NSString alloc]initWithFormat:@"共点%d道菜",num];
    }
}

-(void)goChange:(NSNotification *)notification
{
    [menuTV removeFromSuperview];
    foodNumArray=[[NSMutableArray alloc]init];
    foodMenuArray=[[NSMutableArray alloc]init];
    menuTV=[[UITableView alloc]initWithFrame:CGRectMake(0,0, 320, WXHLApplicationBounds().size.height-64-49) style:UITableViewStylePlain];
    
    NSDictionary *dic = [notification userInfo];
    NSNumber *index = [dic objectForKey:@"index"];
    if ([index integerValue]==0) {
        
        for (int i=0; i<53; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }

        [self initTableView];
    }
    else if ([index integerValue]==1)
    {
        for (int i=53; i<118; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }
//        [foodMenuArray addObject:[NSArray arrayWithObjects:@" ",@" ",@" ",@" ",@" ",@" ",nil]];
        [self initTableView];
        
    }
    else if ([index integerValue]==2)
    {
        for (int i=118; i<165; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }
//        [foodMenuArray addObject:[NSArray arrayWithObjects:@" ",@" ",@" ",@" ",@" ",@" ",nil]];
        [self initTableView];

    }
    else if ([index integerValue]==3)
    {
        for (int i=165; i<217; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }
        [self initTableView];
    }
    else if ([index integerValue]==4)
    {
        for (int i=217; i<300; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }
        [self initTableView];

        
    }
    else if ([index integerValue]==5)
    {
        
        for (int i=300; i<327; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }
//        [foodMenuArray addObject:[NSArray arrayWithObjects:@" ",@" ",@" ",@" ",@" ",@" ",nil]];
        [self initTableView];
        
        
    }
    else if ([index integerValue]==6)
    {
        
        for (int i=327; i<351; i++) {
            [foodMenuArray addObject:elementArray[i]];
            [foodNumArray addObject:@0];
        }
        
        [self initTableView];
        
    }
    else if ([index integerValue]==10)
    {
        
    
        
    }
    else if ([index integerValue]==11)
    {
        
        
        
    }
    else if ([index integerValue]==12)
    {
        
        
        
    }
    else
    {
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeCenter" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"translateCellData" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"translateRightCellData" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showDetialFoodVC" object:nil];
}
@end
